﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Blog.Types.Repository;
using System.Collections;

namespace Blog.Core
{
    public class Message : BaseObject
    {
        private string _MessageText;
        private string _userName;
        
        public Message(Theme theme)
        {
            Theme = theme;
            ThemeId = Theme == null ? 0 : Theme.ID;
        }

        public Message(Theme theme, int id)
            :this(theme)
        {
            _ID = id;
        }

        internal Message(Theme theme, int themeId, IMessage obj)
            : this(theme, obj)
        {
            ThemeId = themeId;
        }

        internal Message(Theme theme, IMessage obj)
            :this(theme)
        {
            CopyFrom(obj);
        }

        private void CopyFrom(IMessage obj)
        {
            _ID = obj.MessageId;
            _MessageText = obj.MessageText;
            MessageTreeId = obj.MessageTreeId;
            ParentId = obj.ParentId;
            ModifyDateTime = obj.ModifyDateTime;
            UserName = obj.UserName;
        }

        private void CopyTo(IMessage obj)
        {
            obj.MessageId = _ID;
            obj.MessageText = _MessageText;
            obj.MessageTreeId = MessageTreeId;
            obj.ParentId = ParentId;
            obj.ModifyDateTime = ModifyDateTime;
            obj.UserName = UserName;
        }

        public override string ToString()
        {
            return _MessageText;
        }

        public string MessageText
        {
            get
            {
                return _MessageText;
            }
            set
            {
                if (_MessageText == value)
                    return;

                _MessageText = value;
                OnPropertyChanged();
            }
        }

        [Browsable(false), ReadOnly(true)]
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (_userName == value)
                    return;

                _userName = value;
                OnPropertyChanged();
            }
        }

        [Browsable(false), ReadOnly(true)]
        public Theme Theme
        {
            get;
            private set;
        }

        [Browsable(false), ReadOnly(true)]
        public int ThemeId
        {
            get;
            private set;
        }
        
        public int MessageTreeId
        {
            get;
            set;
        }

        public int ParentId
        {
            get;
            set;
        }

        [Browsable(false), ReadOnly(true)]
        public DateTime? ModifyDateTime
        {
            get;
            private set;
        }

        protected override void DeleteExecute()
        {
            Factory.Instance.Repository.DeleteMessage(ID);
        }

        protected override void SaveExecute()
        {
            int messageTreeId;
            _ID = Factory.Instance.Repository.Save(ThemeId, ID, delegate(IMessage message) { CopyTo(message); }, out messageTreeId);
            MessageTreeId = messageTreeId;
        }
    }
}
